/*
 * @(#)RespondingRetrieveClients.java
 * Date 2013-10-25
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.xca.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import ihe.iti.xds_b._2007.RespondingGatewayRetrieveService;

import us.hornerscorners.lamppost.config.EndpointConfig;

import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;
import us.hornerscorners.lamppost.ihecommon.config.IHEServiceType;
import us.hornerscorners.lamppost.ihecommon.config.RespondingGatewayConfig;
import us.hornerscorners.lamppost.ihecommon.config.XCAGatewayConfig;

import us.hornerscorners.lamppost.repository.client.RepositoryClient;

import us.hornerscorners.lamppost.repository.service.AtnaRepositoryService;

import us.hornerscorners.lamppost.xca.client.RespondingRetrieveClient;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-10-25
 * @author         Jim Horner
 */
@ApplicationScoped
public class RespondingRetrieveClients implements IRepositoryClients {

    /** Field description */
    @Inject
    private AtnaRepositoryService atnaService;

    /** Field description */
    private final Map<String, RespondingRetrieveClient> clients;

    /** Field description */
    @Inject
    private IHEConfig config;

    /** Field description */
    private final RespondingGatewayRetrieveService respondingService;

    /**
     * Constructs ...
     *
     */
    public RespondingRetrieveClients() {

        super();
        this.respondingService = new RespondingGatewayRetrieveService();
        this.clients = new HashMap<>();
    }

    /**
     * Method description
     *
     *
     * @param homeCommunityId
     *
     * @return
     */
    @Override
    public boolean contains(String homeCommunityId) {

        return this.clients.containsKey(homeCommunityId);
    }

    /**
     * Method description
     *
     *
     * @param homeCommunityId
     *
     * @return
     */
    @Override
    public RespondingRetrieveClient find(String homeCommunityId) {

        return this.clients.get(homeCommunityId);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public RepositoryClient[] findAll() {

        Collection<RespondingRetrieveClient> result = this.clients.values();

        return result.toArray(new RepositoryClient[result.size()]);
    }

    /**
     * Method description
     *
     */
    @PostConstruct
    public void postConstruct() {

        XCAGatewayConfig serviceConfig =
            (XCAGatewayConfig) this.config.findServiceConfig(IHEServiceType.XCA);

        for (RespondingGatewayConfig gwconfig : serviceConfig.getRespondingGateways()) {

            String id = gwconfig.getRetrieveServiceId();
            EndpointConfig endpoint = this.config.findServiceEndpoint(id);

            if (endpoint == null) {

                String msg =
                    String.format(
                        "Unknown retrieveServiceId in XCA Service Config: [%s].", id);

                throw new IllegalStateException(msg);
            }

            String homeId = gwconfig.getHomeCommunityId();
            RespondingRetrieveClient client =
                new RespondingRetrieveClient(this.atnaService, this.respondingService,
                                             homeId, endpoint);

            this.clients.put(homeId, client);
        }
    }
}
